package com.example.nio.filechannel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2023年08月06日 15:14:00
 * @Description
 */
public class Demo05 {

    public static void main(String[] args) throws IOException {
        try(RandomAccessFile f = new RandomAccessFile("test.txt", "rw");
            FileChannel channel = f.getChannel()){
            //截断文件，只留前20个字节
            channel.truncate(20);

            ByteBuffer buffer = ByteBuffer.allocate(128);
            channel.read(buffer);
            buffer.flip();
            System.out.println(new String(buffer.array(), 0, buffer.remaining()));
        }
    }

}
